package com.ysten.basic.common.helper;

import javax.jms.JMSException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ysten.basic.common.utils.SystemConfigLoader;
import com.ysten.basic.common.utils.constant.Constant;
import com.ysten.jms.JmsUtils;
import com.ysten.jms.domain.JmsMessage;

@Service
public class JmsHelper
{
    @Autowired
    private SystemConfigLoader systemConfigLoader;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(JmsHelper.class);
    
    /**
     * <发送JMS消息到JMS服务器>
     * 
     * @param message 发送消息体
     * @param topicName 主题名称
     * @throws JMSException JMS发送消息异常，常见如连接失败，连接超时等
     * @see [类、类#方法、类#成员]
     */
    public boolean sendMessage(JmsMessage message, String topicName)
    {
        boolean result = true;
        // 获取JMS服务器基础信息
        String jmsUserName = systemConfigLoader.getStringValue(Constant.JMS_USER_NAME);
        String jmsPwd = systemConfigLoader.getStringValue(Constant.JSM_PWD);
        String jmsUrl = systemConfigLoader.getStringValue(Constant.JSM_URL);
        JmsUtils jmsUtils = new JmsUtils();
        
        try
        {
            jmsUtils.publish(jmsUserName, jmsPwd, jmsUrl, topicName, message);
            LOGGER.info("发送JMS消息成功，message=" + message.toString());
        }
        catch (JMSException e)
        {
            result = false;
            LOGGER.error("发送JMS消息失败，message=" + message.toString(), e);
        }
        
        return result;
    }
    
}
